package com.example.dllo.exe.fenlei.xuanlishenqi;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.PopupWindowBean;
import com.example.dllo.exe.fengzhuang.DBValue;

/**
 * code is far away from bug with the animal protecting
 * <p>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class PopupwindowAdapter extends BaseAdapter {
    private PopupWindowBean popupWindowBean;
    private Context context;
    private int type;
    private OnItemClickPW onItemClickPW;
    XuanLiShenQiAty xuanLiShenQiActivity;




    public void setXuanLiShenQiActivity(XuanLiShenQiAty xuanLiShenQiActivity) {
        this.xuanLiShenQiActivity = xuanLiShenQiActivity;
    }

    public void setOnItemClickPW(OnItemClickPW onItemClickPW) {
        this.onItemClickPW = onItemClickPW;
    }

    public void setType(int type) {
        this.type = type;
        notifyDataSetChanged();
    }

    public void setPopupWindowBean(PopupWindowBean popupWindowBean) {
        this.popupWindowBean = popupWindowBean;
        notifyDataSetChanged();
    }


    public PopupwindowAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {

        return popupWindowBean != null ? popupWindowBean.getData().getFilters().get(type).getChannels().size() : 0;

    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Log.e("PopupwindowAdapter", "type:" + type);
        ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (view == null) {
            view = inflater.inflate(R.layout.popuplayout, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        viewHolder.textViewgridciew.setText(popupWindowBean.getData().getFilters().get(type).getChannels().get(i).getName());
        viewHolder.textViewgridciew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = DBValue.FENLEI_XUANLISHENQI + "&"+popupWindowBean.getData().getFilters().get(type).getKey() + "=" + popupWindowBean.getData().getFilters().get(type).getChannels().get(i).getId();
                Bundle bundle = new Bundle();
                bundle.putString("key", url);
                String title = popupWindowBean.getData().getFilters().get(type).getKey();
                String key = popupWindowBean.getData().getFilters().get(type).getChannels().get(i).getKey();
                onItemClickPW.OnTtemClickPW(title, key);
               switch (type){
                   case  0:
                xuanLiShenQiActivity.duixiang.setText(popupWindowBean.getData().getFilters().get(0).getChannels().get(i).getName());
                       break;
                   case  1:
                xuanLiShenQiActivity.changhe.setText(popupWindowBean.getData().getFilters().get(1).getChannels().get(i).getName());
                       break;
                   case  2:
                xuanLiShenQiActivity.gexing.setText(popupWindowBean.getData().getFilters().get(2).getChannels().get(i).getName());
                       break;
                   case  3:
                xuanLiShenQiActivity.jiage.setText(popupWindowBean.getData().getFilters().get(3).getChannels().get(i).getName());
                       break;

               }
            }
        });

        return view;
    }

    public class ViewHolder {
        TextView textViewgridciew;

        public ViewHolder(View view) {
            textViewgridciew = (TextView) view.findViewById(R.id.grid_textview);
        }
    }
}
